<?php

namespace app\admin\controller\chat;

use app\admin\model\UM as UM;
use app\admin\service\ConsultingPresetServerService;
use ShidianUtil\Thinkphp\Response\ApiResponse;
use think\APP;
use think\exception\ValidateException;
use think\Request as Request;

/**
 * PresetMemberAskServer 控制器
 * Class PresetMemberAskServer
 * @package app\chat\controller
 * @link https://www.kancloud.cn/manual/thinkphp6_0/1037509
 */
class PresetMemberAskServer
{
    private Request $request;

    private ConsultingPresetServerService $service;

    public function __construct(Request $request, ConsultingPresetServerService $presetConsultingServerService)
    {
        $this->request = $request;
        $this->service = $presetConsultingServerService;
    }

    /**
     * 详情
     * @param \app\admin\model\UM|null $user
     * @return ApiResponse
     * @url /chat/preset_member_ask_server/read
     */
    public function read(?UM $user): ApiResponse
    {
        $request = $this->request;

        $serverType = $request->param('serverType', '2');

        return ApiResponse::success($this->service->read($user, $serverType));
    }

    /**
     * 新增|修改
     * @param \app\admin\model\UM $user
     * @return ApiResponse
     * @url /chat/preset_member_ask_server/save
     */
    public function save(UM $user): ApiResponse
    {
        $request = $this->request;

        if (!$request->has('id')) {
            throw new ValidateException('请选择一个服务');
        }

        $this->service->modify($user, $request->param('id'), $request->param());

        return ApiResponse::success(null);
    }

}
